 //控制层 
app.controller('goodsController' ,function($scope,$controller   ,goodsService,itemCatService,typeTemplateService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		goodsService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		goodsService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		goodsService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}

	//保存
	$scope.save=function(){
        //获取富文本对象
        $scope.entity.tbGoodsDesc.introduction = editor.html();
            goodsService.add( $scope.entity  ).success(

			function(response){
				if(response.success){
					//清空对象
					$scope.entity={};
                    //清空富文本编辑器
                    editor.html('');
                    alert(response.message);
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		goodsService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.reloadList();//刷新列表
					$scope.selectIds=[];
				}						
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		goodsService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
    $scope.showIterm=function (parentId) {
		itemCatService.findByPId(parentId).success(function (data) {
            $scope.topList=data;
        })
    }
    //使用angurlajs监听服务
	//参数1:需要监听的值
	//参数2:newValue新的值,oldValue旧值
	$scope.$watch("entity.goods.category1Id",function (newValue, oldValue) {
        itemCatService.findByPId(newValue).success(function (data) {
            $scope.cat3List=null;
            $scope.cat2List = data;
        })
    })
	$scope.$watch("entity.goods.category2Id",function (newValue, oldValue) {
        itemCatService.findByPId(newValue).success(function (data) {
            $scope.cat3List = data;
        })
    })
    //三级分类选择后 读取模板 ID
    $scope.$watch("entity.goods.category2Id",function (newValue, oldValue) {
        itemCatService.findOne(newValue).success(function (data) {
            $scope.entity.goods.typeTemplateId = data.typeId;
        })
    })

	//根据所查出来的id,使用监听器获取品牌

	$scope.$watch("entity.goods.typeTemplateId",function (newValue, oldValue) {
        typeTemplateService.findOne(newValue).success(function (data) {
			//获取类型模板
			$scope.typeTemplate=data;
			$scope.typeTemplate.brandIds=JSON.parse($scope.typeTemplate.brandIds)
        })
    })
});	
